package com.goddess.yeecolor.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.webview.ruleActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private Map<Object,Object>map;
    private TextView mTitle,start_rule;
    private Toolbar mToobar;
    private View mView;
    private EditText register_phone,register_duanxin,register_password;
    private Button register_send,register_enroll;
    private String phone,duanxin,psd;
    private Bundle bundle;
    private String connect_third,connect_uuid,Tag;//第三方登录的typed uuid
    private TimeCount time;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_register,null);
        bundle=getIntent().getBundleExtra("bundle");
        Tag=bundle.getString("Tag");//判断哪个位置进来的注册
        setContentView(mView);
        initFindView();
        initOnClck();
    }

    @Override
    public void initOnClck() {
        register_enroll.setOnClickListener(this);
        register_send.setOnClickListener(this);
        start_rule.setOnClickListener(this);
    }

    @Override
    public void initFindView() {
        time=new TimeCount(120*1000,1000);
        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        mTitle= (TextView) findViewById(R.id.title_content);
        start_rule= (TextView) findViewById(R.id.start_rule);
        register_phone= (EditText) findViewById(R.id.register_phone);
        register_password= (EditText) findViewById(R.id.register_password);
        register_duanxin= (EditText) findViewById(R.id.register_duanxin);
        register_enroll= (Button) findViewById(R.id.register_enroll);
        register_send= (Button) findViewById(R.id.register_send);
        mTitle.setText("注册");
//        setSupportActionBar(mToobar);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            setFiish(this);
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.register_enroll://注册
                phone=register_phone.getText().toString().trim();
                duanxin=register_duanxin.getText().toString().trim();
                psd=register_password.getText().toString().trim();

                if(!psd.equals("")){
                    map=new HashMap<>();
                    map.put("mobile",phone);
                    map.put("pwd",psd);
                    map.put("code",duanxin);
                }else{
                    ToastUtils.toast("请输入验证码");
                }
                if(Tag.equals("第三方")){//如果是第三方注册,传入下面的参数
                    connect_third=bundle.getString("connect_third");
                    connect_uuid=bundle.getString("connect_uuid");
                    map.put("connect_third",connect_third);
                    map.put("connect_uuid",connect_uuid);

                }
                setRequest(MyUrl.url+MyUrl.register,map,HANDLER_TWO);
                break;
            case R.id.register_send:
                phone=register_phone.getText().toString().trim();
                if(!phone.equals("")){
                    time.start();
                    map=new HashMap<>();
                    map.put("mobile",phone);
                    setRequest(MyUrl.url+MyUrl.member,map,HANDLER_ONE);
                }else{
                    ToastUtils.toast("请输入手机号");
                }

                break;
            case R.id.start_rule:
                JumpIntents.startIntent(this,ruleActivity.class);
                break;
        }
    }

    private final int HANDLER_ONE=1;
    private final int HANDLER_TWO=2;
    private void setRequest(String url, Map<Object,Object>map,int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
    }
    private String jsonString;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    try
                    {
                        jsonString=msg.getData().getString("result");
                        JSONObject object=new JSONObject(jsonString);
                        int code=object.getInt("code");
                        ToastUtils.toast(object.getString("msg"));
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    try
                    {
                        jsonString=msg.getData().getString("result");
                        JSONObject object=new JSONObject(jsonString);
                        int code=object.getInt("code");
                        if (code==100){
                            ToastUtils.toast(object.getString("msg"));
                            JSONObject result=object.getJSONObject("result");
                            String uniique=result.getString("unique");
                            int mobile=result.getInt("mobile");
                            /**
                             * 保存数据
                             */
                            SharePreceUtils.saveRegiter(uniique,mobile);
                            JumpIntents.startIntentCan(RegisterActivity.this,PerfectMeansAcitivity.class,"完善个人资料");
                            finish();
                        }
                        else if (code==403){
                            ToastUtils.toast(object.getString("msg"));
                        }

                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }

        }
    };
    /**
     * 设置时间
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }
        @Override
        public void onTick(long l) {
            register_send.setClickable(false);
            register_send.setBackgroundResource(R.drawable.login_yanzm_button_select_back_false);
            register_send.setText("等待"+l/1000+"秒");
        }
        @Override
        public void onFinish() {
            register_send.setText("重新获取");
            register_send.setBackgroundResource(R.drawable.login_yanzm_button_back);
            register_send.setClickable(true);
        }
    }
}
